<!-- 搜索表单 -->
<template>
  <ele-card :body-style="{ paddingBottom: '2px' }">
    <el-form label-width="72px" @keyup.enter="search" inline>
      <el-form-item label="所属公司">
        <TenantChoose @loaded="search" v-model="form.tenant_id" />
      </el-form-item>

      <br />
      <el-form-item label="所属站点">
        <SiteRadioAll v-model="form.site_id" @change="search" />
      </el-form-item>

      <br />

      <el-form-item label="工作状态">
        <el-radio-group
          v-model="form.dead"
          class="filter-item"
          @change="search"
        >
          <el-radio-button :label="99">全部</el-radio-button>
          <el-radio-button :label="0">进行中</el-radio-button>
          <el-radio-button :label="1">已结束</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <br />

      <el-form-item label="工作编号">
        <el-input clearable v-model.trim="form.code" />
      </el-form-item>

      <el-form-item label="企业名称">
        <el-input clearable v-model.trim="form.company_name" />
      </el-form-item>

      <el-form-item label="岗位名称">
        <el-input clearable v-model.trim="form.title" />
      </el-form-item>

      <el-form-item label="开始日期">
        <el-date-picker
          v-model="form.work_date_s"
          class="filter-item"
          type="date"
          value-format="yyyy-M-d"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="结束日期">
        <el-date-picker
          v-model="form.work_date_e"
          class="filter-item"
          type="date"
          value-format="yyyy-M-d"
          style="width: 150px"
        />
      </el-form-item>

      <el-form-item label-width="16px">
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </ele-card>
</template>

<script setup>
  import { ref } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { listRoles } from '@/api/system/role';
  import { getCityList } from '@/api/system/region';
  import { getTenantList } from '@/api/tenant';

  const emit = defineEmits(['search']);

  /** 表单数据 */
  const [form, resetFields] = useFormData({
    tenant_id: 0,
    company_id: 0,
    company_name: '',
    code: '',
    title: '',
    site_id: 0,
    work_date_e: '',
    work_date_s: '',
    dead: 99
  });

  /** 角色数据 */
  const roleList = ref([]);

  /** 站点数据 */
  const cityList = ref([]);

  /** 搜索 */
  const search = () => {
    emit('search', { ...form });
  };

  /**  重置 */
  const reset = () => {
    resetFields();
    search();
  };
  defineExpose({
    setCompanyName: (value) => {
      console.log(value);
      form.company_name = value;
      search();
    }
  });

  /** 获取角色数据 */
  listRoles({})
    .then((res) => {
      roleList.value = res.list;
    })
    .catch((e) => {
      EleMessage.error(e.message);
    });

  /** 获取租户数据 */
  const tenantList = ref([]);
  getTenantList({})
    .then((res) => {
      tenantList.value = res.list;
      // console.log(res);
      // console.log(form.site_id);
      // assignFields({
      //   site_id: res[0].site_id
      // });
    })
    .catch((e) => {
      EleMessage.error(e.message);
    });

  /** 获取站点数据 */
  getCityList({})
    .then((res) => {
      cityList.value = res.list;
      form.site_id = res.list[0].site_id;
    })
    .catch((e) => {
      EleMessage.error(e.message);
    });
</script>
